package com.shengde.jinwawaforleader.model.homePage.studentproduciton;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.utils.MessageEvent;
import com.shengde.jinwawaforleader.utils.Util;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;

@SuppressLint("UseSparseArrays")
public class LaocalImagesActivity extends Activity {
	Bundle bundle;
	FileTraversal fileTraversal;
	GridView imgGridView;
	LocalImagesAdapter imgsAdapter;
	Util util;
	ArrayList<String> filelist;
	private TextView tv_back, tv_title, fill_add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.local_images);
		fbi();
		initdata();
		listener();

		imgGridView = (GridView) findViewById(R.id.gridView1);
		bundle = getIntent().getExtras();
		fileTraversal = bundle.getParcelable("data");
		Collections.reverse(fileTraversal.subList);//倒序排列
		imgsAdapter = new LocalImagesAdapter(this, fileTraversal.subList, onItemClickClass);
		imgGridView.setAdapter(imgsAdapter);
		filelist = new ArrayList<>();
		util = new Util(this);
	}

	private void fbi() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
	}

	private void initdata() {
		tv_title.setText("选择照片");
		fill_add.setText("完成");
	}

	private void listener() {
		tv_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		fill_add.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (filelist.size() + PictureUtil.newPicturesData.size() > 9) {
					Toast.makeText(LaocalImagesActivity.this, "亲，最多现在9张哦", Toast.LENGTH_SHORT).show();
					return;
				}
				EventBus.getDefault().post(new MessageEvent(MessageEvent.SELECT_LOCAL_PHOTO_OK, filelist));
				finish();
			}
		});
	}

	ImgCallBack imgCallBack = new ImgCallBack() {
		@Override
		public void resultImgCall(ImageView imageView, Bitmap bitmap) {
			imageView.setImageBitmap(bitmap);
		}
	};

	LocalImagesAdapter.OnItemClickClass onItemClickClass = new LocalImagesAdapter.OnItemClickClass() {
		@Override
		public void OnItemClick(View v, int Position, CheckBox checkBox) {
			String filapath = fileTraversal.subList.get(Position);
			if (checkBox.isChecked()) {
				checkBox.setChecked(false);
				filelist.remove(filapath);
				System.out.println(filapath);
				System.out.println("删除");
			} else {
				if (filelist.size() + PictureUtil.newPicturesData.size() <= 8) {
					checkBox.setChecked(true);
					filelist.add(filapath);
				} else {
					Toast.makeText(LaocalImagesActivity.this, "亲，最多现在9张哦", Toast.LENGTH_SHORT).show();
				}
				System.out.println(filapath);
				System.out.println("添加");
			}
		}
	};

	@Override
	public void onStop() {
// 当进程不可见的时候，挂起不需要的剩下的UI更新、线程或者处理，保存所有的编辑或者 
// 状态改变，因为这个进程可能会被销毁(从而为其他进程释资源) 
		super.onStop();
	}
}